//
//  StatueCell.swift
//  sinaWeiBo
//
//  Created by weixq on 15/8/6.
//  Copyright © 2015年 weixq. All rights reserved.
//

import UIKit

let statusCellControlMargin: CGFloat = 8.0

enum StatusCellIdentifier: String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    // 静态函数
    // 根据微博数据，返回对应的饿标示符
    static func cellId(status: Status) -> String {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

class StatueCell: UITableViewCell {

    var status: Status? {
        didSet {
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            pictureView.status = status
            
            pictureHightCons?.constant = pictureView.bounds.size.height
            pictureWidhtCons?.constant = pictureView.bounds.size.width
            pictureTopcons?.constant = (pictureView.bounds.size.height == 0) ? 0 : statusCellControlMargin
            
        }
    }
    
    // 计算行高
    func rowHeight(status: Status) -> CGFloat {

        self.status = status
        layoutIfNeeded()
       return CGRectGetMaxY(bottonView.frame)
    }
    
    // 图片宽度约束
    var pictureWidhtCons: NSLayoutConstraint?
    // 图片高度约束
    var pictureHightCons: NSLayoutConstraint?
    // 配图视图顶部约束
    var pictureTopcons: NSLayoutConstraint?
    
    
    
    // MARK: - 界面设置
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   func setupUI() {
        
        contentView.addSubview(topView )
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottonView)
        contentView.addSubview(pictureView)
        
        // 自动化布局
        // 1. 顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
    
        // 2. 内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: statusCellControlMargin, y: statusCellControlMargin))
    
        // 宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2*statusCellControlMargin))
    
        // 3. 配图视图
//    let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: statusCellControlMargin))
//    
//    pictureWidhtCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//    pictureHightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//    pictureTopcons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
    
        // 4. 底部视图
        bottonView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -statusCellControlMargin, y: statusCellControlMargin))
    
        // 底部约束
        // contentView.addConstraint(NSLayoutConstraint(item: bottonView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -0))

    }
    
    // MARK: - 懒加载控件
    private lazy var topView: StatusTopView = StatusTopView()
    
    // 内容标签
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontsize: 15)
        label.numberOfLines = 0
        
        //设置文本折行的宽度，不设置这个自动化布局会有问题
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 *  statusCellControlMargin
        return label
    }()
    
    // 配图视图
    lazy var pictureView : StatusPictureView = StatusPictureView()
    
    // 底部视图
    lazy var bottonView: StatusButtomView = StatusButtomView()
    
    
}
